//app.js
/*App({
  code:null,
  openid:'',
  onLaunch: function () {
   var that=this;
   wx.login({
      success: function (res) {
        if (res.code) {
          that.code = res.code;
          that.openid=res.openid;
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  }
})*/
//app.js
App({
  json:"",
  unitdoor:"",
  yfilename: "",
  yname: "",
  yphones: "",
  ysex: "",
  address:"",
  ynumber:"",
  yidcar:"",
  owner:{
    ynumber: "",
    yfilename: "",
    yfilename1: "",
    yfilename2: "",
    yname: "",
    yowner: "",
    ysex: "",
    address:"",
    unitdoor:"",
    json:"",
    phone:"",
    unitdoorJson:"",
    yidcar:""
  },

  globalData: {
    isLogin: false,
    userInfo: {},
    jscode: null,
    serverRootUrl: "https://wwl.ycws.cc/wxpay/",
    head: {
      version: "v1.0",
      clientType: "wxxcx",
      apikey: "*****f",
      userId: null,
      resCode: null,
      cmdId: ""
    },
    rawData: null,
    encryptedData: null,
    iv: null,
    signature: null,
    picRootUrl: null,
    openId: null,
    freeFreightFee: null,
    freight: null
  },

  onLaunch: function () {
      this.loginServer()
  },
  getOpenIdTap: function () {
    var that = this;
  
  },
  loginServer: function (e) {
    var that = this;
    //登录成功，设置登录数据
    function setUserData(data) {
      //console.info(data)
      if (data.head.resCode == 0) {

           that.globalData.head.userId = data.body.userInfo.id,
           that.globalData.picRootUrl = data.body.picRoot,
           that.globalData.userInfo = data.body.userInfo,
           that.globalData.isLogin = true,
           that.globalData.openId = data.body.openId,
           that.globalData.freeFreightFee = data.body.freeFreightFee,
           that.globalData.freight = data.body.freight
           console.log("----" + data.body.openId);
           
        
      }else{
        that.globalData.picRootUrl = data.body.picRoot,
          that.globalData.freeFreightFee = data.body.freeFreightFee,
          that.globalData.freight = data.body.freight
          console.log("66");
      }
    }
    function setWxLoginData(res) {
      that.globalData.userInfo = res.userInfo
      that.globalData.rawData = res.rawData
      that.globalData.encryptedData = res.encryptedData
      that.globalData.iv = res.iv,
      that.globalData.signature = res.signature
    }
    //登录
    wx.login({
      success: function (res) {
        that.globalData.jscode = res.code
        wx.getUserInfo({
          success: function (res) {
            setWxLoginData(res)
            requestBaseData();
          
          }
        })
      }
    })
    //请求服务器数据
    function requestBaseData() {
      var bodydata = {
        wxLoginCode: that.globalData.jscode,
        nickName: encodeURIComponent(that.globalData.userInfo.nickName),
        avatarUrl: that.globalData.userInfo.avatarUrl,
        gender: that.globalData.userInfo.gender,
        province: that.globalData.userInfo.province,
        city: that.globalData.userInfo.city,
        country: that.globalData.userInfo.country,
        rawData: encodeURIComponent(that.globalData.rawData),
        signature: that.globalData.signature,
        encryptedData: that.globalData.encryptedData,
        iv: that.globalData.iv
        //yphone:"13424367370"
      }

      wx.request({
        url: 'https://wwl.ycws.cc/wxpay/launch/base',
        //url: 'http://localhost:9090/wxpay/launch/base',
        data: {
          head: that.globalData.head,
          body: bodydata
        },
        method: 'GET', 
        success: function (res) {
          setUserData(res.data)
        }
      })
    }
  }
})
